package com.xin.ruanjian.omsystem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.bean.DishBean;
import com.xin.ruanjian.omsystem.utils.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by local123 on 2018/2/22.
 */

public class GoodsRightAdapter extends RecyclerView.Adapter<GoodsRightAdapter.ViewHolder> {
    private Context context;
    private List<DishBean> mData;
    private onItemClickListener listener;

    public GoodsRightAdapter(Context context) {
        this.context = context;
    }

    public void setListener(onItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<DishBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shop_detail_right_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvShopDetailDishName.setText(mData.get(position).getDname());
        holder.tvShopDetailDishNum.setText(mData.get(position).getNum()+"");
        holder.tvShopDetailDishPrice.setText("¥" + mData.get(position).getDprice());
        GlideUtils.glideLoader(context, mData.get(position).getDpic(),R.mipmap.ic_launcher, R.mipmap.ic_launcher, holder.ivItemShopDetailDish);
        if (mData.get(position).getNum() <= 0){
            holder.ivShopDetailDishSub.setVisibility(View.GONE);
            holder.tvShopDetailDishNum.setVisibility(View.GONE);
        }
        holder.ivShopDetailDishAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddClick(holder, mData.get(position));
                    notifyItemChanged(position);
                }
            }
        });
        holder.ivShopDetailDishSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onSubClick(holder, mData.get(position));
                    notifyItemChanged(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_item_shop_detail_dish)
        ImageView ivItemShopDetailDish;
        @BindView(R.id.tv_shop_detail_dish_name)
        TextView tvShopDetailDishName;
        @BindView(R.id.tv_shop_detail_dish_price)
        TextView tvShopDetailDishPrice;
        @BindView(R.id.iv_shop_detail_dish_sub)
        public ImageView ivShopDetailDishSub;
        @BindView(R.id.tv_shop_detail_dish_num)
        public TextView tvShopDetailDishNum;
        @BindView(R.id.iv_shop_detail_dish_add)
        public ImageView ivShopDetailDishAdd;
        @BindView(R.id.ll_item_right_add_sub)
        LinearLayout llItemRightAddSub;
        @BindView(R.id.tv_item_right_guige)
        TextView tvItemRightGuige;
        @BindView(R.id.tv_item_right_num)
        TextView tvItemRightNum;
        @BindView(R.id.rl_right)
        LinearLayout rlRight;
        @BindView(R.id.tv_xiajia)
        TextView tvXiajia;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface onItemClickListener {
        void onAddClick(ViewHolder holder, DishBean bean);
        void onSubClick(ViewHolder holder, DishBean bean);
    }
}
